package com.ruoyi.project.system.domain;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.ruoyi.framework.aspectj.lang.annotation.Excel;
import lombok.Data;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

/**
 * 通用任务数据传输对象
 * 
 * @author ruoyi
 * @date 2025-06-16
 */
@Data
public class Tydto {
    /** 通用ID字段 */
    private Long id;

    /** ==================== Tasks实体相关字段 ==================== */
    /** 任务ID */
    private Long taskId;
    /** 任务标题 */
    private String title;
    /** 任务描述 */
    private String description;
    /** 任务创建者ID */
    private Long creatorId;
    /** 任务创建者名字 */
    private String creatorName;
    /** 任务创建者单位ID */
    private Long deptId;
    /** 任务类型（0=日常任务，1=紧急任务，2=临时任务） */
    private String taskType;
    /** 任务优先级（1=低，2=中，3=高，4=紧急） */
    private String priority;
    /** 任务状态 */
    private String status;
    /** 任务截止时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date deadline;
    /** 任务是否激活状态（true=激活，false=停用） */
    @Excel(name = "任务是否激活状态", readConverterExp = "t=rue=激活，false=停用")
    private Integer isActive;
    /** 任务生效开始时间（为空表示立即生效） */
    private String effectiveStart;
    /** 任务生效结束时间（为空表示永久有效） */
    private String effectiveEnd;
    /** 任务创建时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date createdAt;
    /** 最后更新时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date updatedAt;
    /** 任务相关人员列表 */
    private List<Map<String, Object>> members;
    /** 推送日志列表 */
    private List<ReminderLogs> pushLogs;
    /** 任务接收人总数 */
    private Integer totalRecipients;
    /** 任务完成人数 */
    private Integer completedRecipients;
    /** 任务完成率 */
    private double completionRate;

    /** ==================== TaskRecipients实体相关字段 ==================== */
    /** 接收人ID */
    private Long userId;
    /** 接收人用户名 */
    private String userName;
    /** 接收人昵称 */
    private String nickName;
    /** 接收人头像 */
    private String avatar;
    /** 接收人ID数组 */
    private Long[] recipientIds;

    /** ==================== ReminderRules实体相关字段 ==================== */
    /** 提醒频率 */
    private String frequency;
    /** 提醒时间 */
    private String timeOfDay;
    /** 每周提醒日（weekly频率时使用）[1,2,3,4,5]表示周一到周五 */
    private String weeklyDays;
    /** 每月提醒日（monthly频率时使用）[1,15]表示每月1号和15号 */
    private String monthlyDays;
    /** 每年提醒月份（yearly频率时使用）[1,6,12]表示1月、6月、12月 */
    private String yearlyMonths;
    /** 每年提醒日（yearly频率时使用）[15]表示各月15号 */
    private String yearlyDays;
    /** 自定义规则（custom频率时使用）（cron语法） */
    private String customRule;
    /** 周期截止日期（前端传递的字符串格式） */
    private String cycleDeadline;
    /** 周期截止时间相关参数 */
    private Integer cycleDeadlineWeekDay;
    private Integer cycleDeadlineMonthDay;
    private Integer cycleDeadlineYearMonth;
    private Integer cycleDeadlineYearDay;
    /** 下次触发提醒的时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date nextTrigger;
    /** 上次触发时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date lastTriggered;

    /** ==================== ReminderLogs实体相关字段 ==================== */
    /** 提醒日志ID */
    private Long reminderLogId;
    /** 计划提醒时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date scheduledTime;
    /** 提醒发送状态 */
    private String reminderStatus;
    /** 任务完成状态 */
    private String completionStatus;
    /** 任务完成人 */
    private Long completedBy;
    /** 任务完成时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date completedAt;

    /** ==================== TaskCycleDeadlines实体相关字段 ==================== */
    /** 周期ID */
    private Integer cycleId;
    /** 周期类型 */
    private String cycleType;
    /** 周期开始时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date cycleStart;
    /** 周期结束时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date cycleEnd;
    /** 该周期的任务截止时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date cycleDeadlineDate;
    /** 是否是当前周期（1=是，0=否） */
    private Integer isCurrent;
    /** 周期创建时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date cycleCreatedAt;
    /** 周期更新时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date cycleUpdatedAt;

    /** ==================== UserTaskCompletion实体相关字段 ==================== */
    /** 用户任务完成状态ID */
    private Integer userTaskCompletionId;
    /** 用户任务完成状态 */
    private String userTaskCompletionStatus;
    /** 用户任务完成时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date userTaskCompletedAt;
    /** 用户任务完成状态创建时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date userTaskCompletionCreatedAt;
    /** 用户任务完成状态更新时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date userTaskCompletionUpdatedAt;

    /** ==================== Notification实体相关字段 ==================== */
    /** 通知ID */
    private Long notificationId;
    /** 通知类型：weather(天气预警)、deadline(过期预警)、notification(普通消息) */
    private String type;
    /** 通知内容 */
    private String content;
    /** 是否已读：0-未读，1-已读 */
    private Long isRead;
    /** 阅读时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date readTime;
    /** 未读数量 */
    private Integer count;
    /** 是否未读 */
    private Boolean unread;
    /** 通知时间（前端显示用） */
    private String time;

    /** ==================== Content实体相关字段 ==================== */
    /** 内容摘要 */
    private String summary;
    /** 内容封面图片 */
    private String coverImage;
    /** 浏览量 */
    private Long viewCount;
    /** 发布时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date publishTime;
    /** 内容所属栏目ID */
    private Long categoryId;
    /** 内容所属栏目信息 */
    private ContentCategory category;
    /** 内容附件列表 */
    private List<ContentAttachment> attachments;
    /** 内容联系人列表 */
    private List<ContentContact> contacts;
    /** 内容ID（用于添加附件和联系人） */
    private Long contentId;

    /** ==================== ContentAttachment实体相关字段 ==================== */
    /** 附件名称 */
    private String name;
    /** 附件URL */
    private String url;
    /** 文件类型 */
    private String fileType;
    /** 文件大小 */
    private Long fileSize;
    /** 排序 */
    private Long sort;

    /** ==================== ContentContact实体相关字段 ==================== */
    /** 联系人角色 */
    private String role;
    /** 联系人电话 */
    private String phone;

    /** ==================== PushMessageRequest相关字段 ==================== */
    /** 设备ID */
    private String deviceId;

    /** ==================== 随手拍相关字段 ==================== */
    /** 位置信息 */
    private String location;
    /** 申报人 */
    private String reporter;
    /** 联系方式 */
    private String contact;
    /** 图片URL列表 */
    private List<String> imageUrls;
    /** 处理人员姓名 */
    private String processorName;
    /** 处理时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date processTime;
    /** 处理备注 */
    private String processRemark;
    /** 创建时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date createTime;
    /** 分页参数：页码 */
    private Integer pageNum;
    /** 分页参数：每页条数 */
    private Integer pageSize;

    /** ==================== 其他辅助字段 ==================== */
    /** 图片列表 */
    private List<String> images;
    /** 文档列表 */
    private List<Map<String, Object>> documents;
    /** 所有周期 */
    private List<TaskCycleDeadlines> cycleAllList;
    /** 所有完成情况 */
    private List<UserTaskCompletion> userCompletionAllList;
    /** 所有完成情况（包含用户信息） */
    private List<Map<String, Object>> userCompletionWithInfoList;

    /**
     * 从 Tasks 对象构造 Tydto
     * 
     * @param tasks Tasks对象
     * @return Tydto对象
     */
    public static Tydto fromTasks(Tasks tasks) {
        if (tasks == null) {
            return null;
        }

        Tydto dto = new Tydto();
        dto.setTaskId(tasks.getId());
        dto.setTitle(tasks.getTitle());
        dto.setDescription(tasks.getDescription());
        dto.setCreatorId(tasks.getCreatorId());
        dto.setDeptId(tasks.getDeptId());
        dto.setTaskType(tasks.getTaskType());
        dto.setPriority(tasks.getPriority());
        dto.setDeadline(tasks.getDeadline());
        dto.setIsActive(tasks.getIsActive());
        dto.setStatus(tasks.getStatus());

        // 处理日期格式
        if (tasks.getEffectiveStart() != null) {
            dto.setEffectiveStart(new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                    .format(tasks.getEffectiveStart()));
        }
        if (tasks.getEffectiveEnd() != null) {
            dto.setEffectiveEnd(new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                    .format(tasks.getEffectiveEnd()));
        }

        dto.setCreatedAt(tasks.getCreatedAt());
        dto.setUpdatedAt(tasks.getUpdatedAt());

        // 初始化members列表
        dto.setMembers(new ArrayList<>());

        return dto;
    }

    /**
     * 从 Notification 对象构造 Tydto
     * 
     * @param notification     Notification对象
     * @param userNotification UserNotification对象
     * @return Tydto对象
     */
    public static Tydto fromNotification(Notification notification, UserNotification userNotification) {
        if (notification == null) {
            return null;
        }

        Tydto dto = new Tydto();
        dto.setNotificationId(notification.getId());
        dto.setType(notification.getType());
        dto.setTitle(notification.getTitle());
        dto.setContent(notification.getContent());
        dto.setCreatedAt(notification.getCreateTime());

        // 设置已读状态
        if (userNotification != null) {
            dto.setIsRead(userNotification.getIsRead());
            dto.setReadTime(userNotification.getReadTime());
            dto.setUnread(userNotification.getIsRead() == null || userNotification.getIsRead() == 0);
        } else {
            dto.setUnread(true);
        }

        return dto;
    }

    /**
     * 从 Content 对象构造 Tydto
     * 
     * @param content Content对象
     * @return Tydto对象
     */
    public static Tydto fromContent(Content content) {
        if (content == null) {
            return null;
        }

        Tydto dto = new Tydto();
        dto.setId(content.getId());
        dto.setCategoryId(content.getCategoryId());
        dto.setTitle(content.getTitle());
        dto.setSummary(content.getSummary());
        dto.setContent(content.getContent());
        dto.setCoverImage(content.getCoverImage());
        dto.setViewCount(content.getViewCount());
        dto.setPublishTime(content.getPublishTime());
        dto.setCreatorId(content.getCreatorId());
        dto.setStatus(content.getStatus() != null ? content.getStatus().toString() : null);
        dto.setCreatedAt(content.getCreateTime());
        dto.setUpdatedAt(content.getUpdateTime());

        return dto;
    }

    /**
     * 从 Content 对象构造 Tydto，并包含附件和联系人信息
     * 
     * @param content     Content对象
     * @param attachments 附件列表
     * @param contacts    联系人列表
     * @param category    栏目信息
     * @return Tydto对象
     */
    public static Tydto fromContent(Content content, List<ContentAttachment> attachments,
            List<ContentContact> contacts, ContentCategory category) {
        Tydto dto = fromContent(content);
        if (dto == null) {
            return null;
        }

        dto.setAttachments(attachments);
        dto.setContacts(contacts);
        dto.setCategory(category);

        if (category != null) {
            dto.setTaskType(category.getCode());
        }

        return dto;
    }

    /**
     * 从 ReminderRules 对象构造 Tydto
     * 
     * @param rules ReminderRules对象
     * @return Tydto对象
     */
    public static Tydto fromReminderRules(ReminderRules rules) {
        if (rules == null) {
            return null;
        }

        Tydto dto = new Tydto();
        dto.setTaskId(rules.getTaskId());
        dto.setFrequency(rules.getFrequency());

        // 处理时间格式
        if (rules.getTimeOfDay() != null) {
            dto.setTimeOfDay(new java.text.SimpleDateFormat("HH:mm:ss").format(rules.getTimeOfDay()));
        }

        dto.setNextTrigger(rules.getNextTrigger());
        dto.setWeeklyDays(rules.getWeeklyDays());
        dto.setMonthlyDays(rules.getMonthlyDays());
        dto.setYearlyMonths(rules.getYearlyMonths());
        dto.setYearlyDays(rules.getYearlyDays());
        dto.setCustomRule(rules.getCustomRule());
        dto.setLastTriggered(rules.getLastTriggered());
        dto.setCreatedAt(rules.getCreatedAt());
        dto.setUpdatedAt(rules.getUpdatedAt());

        return dto;
    }

    /**
     * 从 ReminderLogs 对象构造 Tydto
     * 
     * @param logs ReminderLogs对象
     * @return Tydto对象
     */
    public static Tydto fromReminderLogs(ReminderLogs logs) {
        if (logs == null) {
            return null;
        }

        Tydto dto = new Tydto();
        dto.setReminderLogId(logs.getId());
        dto.setTaskId(logs.getTaskId());
        dto.setScheduledTime(logs.getScheduledTime());
        dto.setReminderStatus(logs.getStatus());
        dto.setCompletionStatus(logs.getCompletionStatus());
        dto.setCompletedBy(logs.getCompletedBy());
        dto.setCompletedAt(logs.getCompletedAt());
        dto.setCreatedAt(logs.getCreatedAt());
        dto.setUpdatedAt(logs.getUpdatedAt());

        return dto;
    }

    /**
     * 从 TaskRecipients 对象构造 Tydto
     * 
     * @param recipient TaskRecipients对象
     * @return Tydto对象
     */
    public static Tydto fromTaskRecipients(TaskRecipients recipient) {
        if (recipient == null) {
            return null;
        }

        Tydto dto = new Tydto();
        dto.setTaskId(recipient.getTaskId());
        dto.setUserId(recipient.getUserId());

        return dto;
    }

    /**
     * 从 ContentAttachment 对象构造 Tydto
     * 
     * @param attachment ContentAttachment对象
     * @return Tydto对象
     */
    public static Tydto fromContentAttachment(ContentAttachment attachment) {
        if (attachment == null) {
            return null;
        }

        Tydto dto = new Tydto();
        dto.setId(attachment.getId());
        dto.setContentId(attachment.getContentId());
        dto.setName(attachment.getName());
        dto.setUrl(attachment.getUrl());
        dto.setFileType(attachment.getFileType());
        dto.setFileSize(attachment.getFileSize());
        dto.setSort(attachment.getSort());
        dto.setCreatedAt(attachment.getCreateTime());

        return dto;
    }

    /**
     * 从 ContentContact 对象构造 Tydto
     * 
     * @param contact ContentContact对象
     * @return Tydto对象
     */
    public static Tydto fromContentContact(ContentContact contact) {
        if (contact == null) {
            return null;
        }

        Tydto dto = new Tydto();
        dto.setId(contact.getId());
        dto.setContentId(contact.getContentId());
        dto.setName(contact.getName());
        dto.setRole(contact.getRole());
        dto.setPhone(contact.getPhone());
        dto.setSort(contact.getSort());
        dto.setCreatedAt(contact.getCreateTime());

        return dto;
    }

    /**
     * 从 ContentCategory 对象构造 Tydto
     * 
     * @param category ContentCategory对象
     * @return Tydto对象
     */
    public static Tydto fromContentCategory(ContentCategory category) {
        if (category == null) {
            return null;
        }

        Tydto dto = new Tydto();
        dto.setId(category.getId());
        dto.setName(category.getName());
        dto.setTaskType(category.getCode());
        dto.setCoverImage(category.getIcon());
        dto.setSort(category.getSort());
        dto.setStatus(category.getStatus() != null ? category.getStatus().toString() : null);
        dto.setCreatedAt(category.getCreateTime());

        return dto;
    }

    /**
     * 将Tydto转换为Tasks对象
     * 
     * @return Tasks对象
     */
    public Tasks toTasks() {
        Tasks tasks = new Tasks();

        if (this.taskId != null) {
            tasks.setId(this.taskId);
        } else if (this.id != null) {
            tasks.setId(this.id);
        }

        tasks.setTitle(this.title);
        tasks.setDescription(this.description);
        tasks.setCreatorId(this.creatorId);
        tasks.setDeptId(this.deptId);
        tasks.setTaskType(this.taskType);
        tasks.setPriority(this.priority);
        tasks.setStatus(this.status);
        tasks.setDeadline(this.deadline);
        tasks.setIsActive(this.isActive);

        // 处理日期格式
        if (this.effectiveStart != null) {
            try {
                tasks.setEffectiveStart(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(this.effectiveStart));
            } catch (ParseException e) {
                // 日期解析失败时不设置该字段
            }
        }

        if (this.effectiveEnd != null) {
            try {
                tasks.setEffectiveEnd(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(this.effectiveEnd));
            } catch (ParseException e) {
                // 日期解析失败时不设置该字段
            }
        }

        tasks.setCreatedAt(this.createdAt);
        tasks.setUpdatedAt(this.updatedAt);

        return tasks;
    }

    /**
     * 将Tydto转换为ReminderRules对象
     * 
     * @return ReminderRules对象
     */
    public ReminderRules toReminderRules() {
        ReminderRules rules = new ReminderRules();

        rules.setTaskId(this.taskId);
        rules.setFrequency(this.frequency);

        // 处理时间格式
        if (this.timeOfDay != null) {
            try {
                rules.setTimeOfDay(new SimpleDateFormat("HH:mm:ss").parse(this.timeOfDay));
            } catch (ParseException e) {
                // 日期解析失败时不设置该字段
            }
        }

        rules.setNextTrigger(this.nextTrigger);
        rules.setWeeklyDays(this.weeklyDays);
        rules.setMonthlyDays(this.monthlyDays);
        rules.setYearlyMonths(this.yearlyMonths);
        rules.setYearlyDays(this.yearlyDays);
        rules.setCustomRule(this.customRule);
        rules.setLastTriggered(this.lastTriggered);
        rules.setCreatedAt(this.createdAt);
        rules.setUpdatedAt(this.updatedAt);

        return rules;
    }

    /**
     * 将Tydto转换为ReminderLogs对象
     * 
     * @return ReminderLogs对象
     */
    public ReminderLogs toReminderLogs() {
        ReminderLogs logs = new ReminderLogs();

        if (this.reminderLogId != null) {
            logs.setId(this.reminderLogId);
        }

        logs.setTaskId(this.taskId);
        logs.setScheduledTime(this.scheduledTime);
        logs.setStatus(this.reminderStatus);
        logs.setCompletionStatus(this.completionStatus);
        logs.setCompletedBy(this.completedBy);
        logs.setCompletedAt(this.completedAt);
        logs.setCreatedAt(this.createdAt);
        logs.setUpdatedAt(this.updatedAt);

        return logs;
    }

    /**
     * 将Tydto转换为TaskRecipients对象
     * 
     * @return TaskRecipients对象
     */
    public TaskRecipients toTaskRecipients() {
        TaskRecipients recipient = new TaskRecipients();

        recipient.setTaskId(this.taskId);
        recipient.setUserId(this.userId);

        return recipient;
    }

    /**
     * 将Tydto转换为Notification对象
     * 
     * @return Notification对象
     */
    public Notification toNotification() {
        Notification notification = new Notification();

        if (this.notificationId != null) {
            notification.setId(this.notificationId);
        }

        notification.setType(this.type);
        notification.setTitle(this.title);
        notification.setContent(this.content);

        if (this.status != null) {
            try {
                notification.setStatus(Long.parseLong(this.status));
            } catch (NumberFormatException e) {
                // 转换失败时不设置该字段
            }
        }

        return notification;
    }

    /**
     * 将Tydto转换为UserNotification对象
     * 
     * @return UserNotification对象
     */
    public UserNotification toUserNotification() {
        UserNotification userNotification = new UserNotification();

        userNotification.setUserId(this.userId);
        userNotification.setNotificationId(this.notificationId);
        userNotification.setIsRead(this.isRead);
        userNotification.setReadTime(this.readTime);

        return userNotification;
    }

    /**
     * 将Tydto转换为Content对象
     * 
     * @return Content对象
     */
    public Content toContent() {
        Content content = new Content();

        if (this.contentId != null) {
            content.setId(this.contentId);
        } else if (this.id != null) {
            content.setId(this.id);
        }

        content.setCategoryId(this.categoryId);
        content.setTitle(this.title);
        content.setSummary(this.summary);
        content.setContent(this.content);
        content.setCoverImage(this.coverImage);
        content.setCreatorId(this.creatorId);
        content.setViewCount(this.viewCount);

        if (this.status != null) {
            try {
                content.setStatus(Long.parseLong(this.status));
            } catch (NumberFormatException e) {
                // 转换失败时不设置该字段
            }
        }

        content.setPublishTime(this.publishTime);

        return content;
    }

    /**
     * 将Tydto转换为ContentAttachment对象
     * 
     * @return ContentAttachment对象
     */
    public ContentAttachment toContentAttachment() {
        ContentAttachment attachment = new ContentAttachment();

        if (this.id != null) {
            attachment.setId(this.id);
        }

        attachment.setContentId(this.contentId);
        attachment.setName(this.name);
        attachment.setUrl(this.url);
        attachment.setFileType(this.fileType);
        attachment.setFileSize(this.fileSize);
        attachment.setSort(this.sort);

        return attachment;
    }

    /**
     * 将Tydto转换为ContentContact对象
     * 
     * @return ContentContact对象
     */
    public ContentContact toContentContact() {
        ContentContact contact = new ContentContact();

        if (this.id != null) {
            contact.setId(this.id);
        }

        contact.setContentId(this.contentId);
        contact.setName(this.name);
        contact.setRole(this.role);
        contact.setPhone(this.phone);
        contact.setSort(this.sort);

        return contact;
    }

    /**
     * 将Tydto转换为ContentCategory对象
     * 
     * @return ContentCategory对象
     */
    public ContentCategory toContentCategory() {
        ContentCategory category = new ContentCategory();

        if (this.id != null) {
            category.setId(this.id);
        }

        category.setName(this.name);
        category.setCode(this.taskType);
        category.setIcon(this.coverImage);
        category.setSort(this.sort);

        if (this.status != null) {
            try {
                category.setStatus(Long.parseLong(this.status));
            } catch (NumberFormatException e) {
                // 转换失败时不设置该字段
            }
        }

        return category;
    }

    /**
     * 将Tydto转换为PushMessageRequest对象
     * 
     * @return PushMessageRequest对象
     */
    public PushMessageRequest toPushMessageRequest() {
        PushMessageRequest request = new PushMessageRequest();

        request.setTitle(this.title);
        request.setContent(this.content);
        request.setTarget(this.deviceId);

        return request;
    }

    /**
     * 从 SysSnapshot 对象构造 Tydto
     * 
     * @param sysSnapshot SysSnapshot对象
     * @return Tydto对象
     */
    public static Tydto fromSysSnapshot(SysSnapshot sysSnapshot) {
        if (sysSnapshot == null) {
            return null;
        }

        Tydto dto = new Tydto();
        dto.setId(sysSnapshot.getId());
        dto.setLocation(sysSnapshot.getLocation());
        dto.setReporter(sysSnapshot.getReporter());
        dto.setContact(sysSnapshot.getContact());
        dto.setDescription(sysSnapshot.getDescription());
        dto.setStatus(String.valueOf(sysSnapshot.getStatus()));
        dto.setCreateTime(sysSnapshot.getCreateTime());
        dto.setProcessorName(sysSnapshot.getProcessorName());
        dto.setProcessTime(sysSnapshot.getProcessTime());
        dto.setProcessRemark(sysSnapshot.getProcessRemark());

        // 处理图片列表
        if (sysSnapshot.getImages() != null && !sysSnapshot.getImages().isEmpty()) {
            List<String> imageUrls = new ArrayList<>();
            for (SysSnapshotImage image : sysSnapshot.getImages()) {
                imageUrls.add(image.getImageUrl());
            }
            dto.setImageUrls(imageUrls);
        }

        return dto;
    }

    /**
     * 转换为 SysSnapshot 对象
     * 
     * @return SysSnapshot对象
     */
    public SysSnapshot toSysSnapshot() {
        SysSnapshot sysSnapshot = new SysSnapshot();
        sysSnapshot.setId(this.id);
        sysSnapshot.setLocation(this.location);
        sysSnapshot.setReporter(this.reporter);
        sysSnapshot.setContact(this.contact);
        sysSnapshot.setDescription(this.description);
        if (this.status != null) {
            try {
                sysSnapshot.setStatus(Integer.parseInt(this.status));
            } catch (NumberFormatException e) {
                // 默认为待处理状态
                sysSnapshot.setStatus(0);
            }
        }
        sysSnapshot.setProcessorName(this.processorName);
        sysSnapshot.setProcessTime(this.processTime);
        sysSnapshot.setProcessRemark(this.processRemark);

        return sysSnapshot;
    }

    /**
     * 从 TaskCycleDeadlines 对象构造 Tydto
     * 
     * @param cycleDeadlines TaskCycleDeadlines对象
     * @return Tydto对象
     */
    public static Tydto fromTaskCycleDeadlines(TaskCycleDeadlines cycleDeadlines) {
        if (cycleDeadlines == null) {
            return null;
        }

        Tydto dto = new Tydto();
        dto.setCycleId(cycleDeadlines.getId());
        dto.setTaskId(cycleDeadlines.getTaskId());
        dto.setCycleType(cycleDeadlines.getCycleType());
        dto.setCycleStart(cycleDeadlines.getCycleStart());
        dto.setCycleEnd(cycleDeadlines.getCycleEnd());
        dto.setCycleDeadlineDate(cycleDeadlines.getDeadline());
        dto.setIsCurrent(cycleDeadlines.getIsCurrent());
        dto.setNextTrigger(cycleDeadlines.getNextTrigger());
        dto.setCycleCreatedAt(cycleDeadlines.getCreatedAt());
        dto.setCycleUpdatedAt(cycleDeadlines.getUpdatedAt());

        return dto;
    }

    public void buildTaskCycleDeadlines(TaskCycleDeadlines cycleDeadlines) {
        this.cycleId = cycleDeadlines.getId();
        this.cycleType = cycleDeadlines.getCycleType();
        this.cycleStart = cycleDeadlines.getCycleStart();
        this.cycleEnd = cycleDeadlines.getCycleEnd();
        this.cycleDeadlineDate = cycleDeadlines.getDeadline();
        this.isCurrent = cycleDeadlines.getIsCurrent();
        this.nextTrigger = cycleDeadlines.getNextTrigger();
        this.cycleCreatedAt = cycleDeadlines.getCreatedAt();
        this.cycleUpdatedAt = cycleDeadlines.getUpdatedAt();
    }

    /**
     * 从 UserTaskCompletion 对象构造 Tydto
     * 
     * @param userTaskCompletion UserTaskCompletion对象
     * @return Tydto对象
     */
    public static Tydto fromUserTaskCompletion(UserTaskCompletion userTaskCompletion) {
        if (userTaskCompletion == null) {
            return null;
        }

        Tydto dto = new Tydto();
        dto.setUserTaskCompletionId(userTaskCompletion.getId());
        dto.setTaskId(userTaskCompletion.getTaskId().longValue());
        dto.setUserId(userTaskCompletion.getUserId());
        dto.setCycleId(userTaskCompletion.getCycleId());
        dto.setUserTaskCompletionStatus(userTaskCompletion.getStatus());
        dto.setUserTaskCompletedAt(userTaskCompletion.getCompletedAt());
        dto.setUserTaskCompletionCreatedAt(userTaskCompletion.getCreatedAt());
        dto.setUserTaskCompletionUpdatedAt(userTaskCompletion.getUpdatedAt());

        return dto;
    }

    /**
     * 直接将 UserTaskCompletion 对象字段赋值到当前 Tydto 实例
     * 
     * @param userTaskCompletion UserTaskCompletion对象
     */
    public void buildUserTaskCompletion(UserTaskCompletion userTaskCompletion) {
        if (userTaskCompletion == null) {
            return;
        }
        this.userTaskCompletionId = userTaskCompletion.getId();
        this.userTaskCompletionStatus = userTaskCompletion.getStatus();
        this.userTaskCompletedAt = userTaskCompletion.getCompletedAt();
        this.userTaskCompletionCreatedAt = userTaskCompletion.getCreatedAt();
        this.userTaskCompletionUpdatedAt = userTaskCompletion.getUpdatedAt();
    }

    /**
     * 将Tydto转换为TaskCycleDeadlines对象
     * 
     * @return TaskCycleDeadlines对象
     */
    public TaskCycleDeadlines toTaskCycleDeadlines() {
        TaskCycleDeadlines cycleDeadlines = new TaskCycleDeadlines();

        if (this.cycleId != null) {
            cycleDeadlines.setId(this.cycleId);
        }

        cycleDeadlines.setTaskId(this.taskId);
        cycleDeadlines.setCycleType(this.cycleType);
        cycleDeadlines.setCycleStart(this.cycleStart);
        cycleDeadlines.setCycleEnd(this.cycleEnd);
        cycleDeadlines.setDeadline(this.cycleDeadlineDate);
        cycleDeadlines.setIsCurrent(this.isCurrent);
        cycleDeadlines.setNextTrigger(this.nextTrigger);
        cycleDeadlines.setCreatedAt(this.cycleCreatedAt);
        cycleDeadlines.setUpdatedAt(this.cycleUpdatedAt);

        return cycleDeadlines;
    }

    /**
     * 将Tydto转换为UserTaskCompletion对象
     * 
     * @return UserTaskCompletion对象
     */
    public UserTaskCompletion toUserTaskCompletion() {
        UserTaskCompletion userTaskCompletion = new UserTaskCompletion();

        if (this.userTaskCompletionId != null) {
            userTaskCompletion.setId(this.userTaskCompletionId);
        }

        if (this.taskId != null) {
            userTaskCompletion.setTaskId(this.taskId.intValue());
        }
        userTaskCompletion.setUserId(this.userId);
        userTaskCompletion.setCycleId(this.cycleId);
        userTaskCompletion.setStatus(this.userTaskCompletionStatus);
        userTaskCompletion.setCompletedAt(this.userTaskCompletedAt);
        userTaskCompletion.setCreatedAt(this.userTaskCompletionCreatedAt);
        userTaskCompletion.setUpdatedAt(this.userTaskCompletionUpdatedAt);

        return userTaskCompletion;
    }
}